package com.cws4cloud.cws.sdAdmin.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cws4cloud.cws.common.core.util.R;
import com.cws4cloud.cws.common.log.annotation.SysLog;
import com.cws4cloud.cws.sdAdmin.api.entity.Company;
import com.cws4cloud.cws.sdAdmin.api.service.CompanyService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 
 *
 * @author zhulj
 * @date 2021-05-10 23:09:56
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/company")
@Api(value = "company", tags = "商家管理")
public class CompanyController {

    private final  CompanyService companyService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param company 
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('sdAdmin_company_get')" )
    public R getCompanyPage(Page page, Company company) {
        return R.ok(companyService.page(page, Wrappers.query(company)));
    }


    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sdAdmin_company_get')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(companyService.getById(id));
    }

    /**
     * 新增
     * @param company 
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sdAdmin_company_add')" )
    public R save(@RequestBody Company company) {
        return R.ok(companyService.save(company));
    }

    /**
     * 修改
     * @param company 
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sdAdmin_company_edit')" )
    public R updateById(@RequestBody Company company) {
        return R.ok(companyService.updateById(company));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sdAdmin_company_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(companyService.removeById(id));
    }

}
